package com.qq.financeSystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.financeSystem.model.Offset;
import com.qq.financeSystem.model.YearAndMonth;
import com.qq.financeSystem.service.OffsetService;
import com.qq.financeSystem.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/offset")
public class OffsetController {

    @Autowired
    OffsetService offsetService;

    //查询本月所有报销（部门ID）
    @RequestMapping("/showAllNowOffset")
    public String showAllNowOffset(ModelMap m, @RequestParam(defaultValue = "1", value = "ident") String ident,
                                   @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Offset> offsets = offsetService.queryAllOffset(ident);
        PageInfo<Offset> pageInfo = new PageInfo<>(offsets);
        m.addAttribute("pageInfo", pageInfo);
        m.addAttribute("ident", ident);
        return "lsq/offset/offsetSystem";
    }

    //跳历史查询页面
    @RequestMapping("/selectSalary")
    public String selectSalary() {
        return "redirect:showAllOffsetByKey";
    }

    //指定历史年月部门津贴查询（识别码，查询条件跟着走）
    @RequestMapping("/showAllOffsetByKey")
    public String showAllSalaryByKey(YearAndMonth key, ModelMap m, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Offset> offsets = offsetService.queryOffsetByKey(key);
        PageInfo<Offset> pageInfo = new PageInfo<>(offsets);
        m.addAttribute("pageInfo", pageInfo);
        //查询条件跟着页面走
        m.addAttribute("Key", key);
        return "lsq/offset/selectOffsetSystem";
    }

    //个人历史查询（模态框）
    @ResponseBody
    @RequestMapping("/showLastOneById")
    public List<Offset> showLastOneById(String empId) {
        return offsetService.selectLastOneById(empId);
    }

    //跳添加界面
    @RequestMapping("/addOffsetPag")
    public String addOffsetPag() {
        return "lsq/offset/addOffset";
    }

    //添加报销信息
    @RequestMapping("/addOffset")
    @ResponseBody
    public ResultInfo addOffset(Offset offset) {
        System.out.println(offset.getEmpName()+","+offset.getEmpId());
        if (offset.getEmpName().equals("") || offset.getEmpId().equals("") || offset.getMatters().equals("") || offset.getOffsetMoney().equals(""))
            return new ResultInfo("480", "请填写所有信息");
        String ident = offsetService.queryOneByIdAndName(offset.getEmpId(), offset.getEmpName());
        if (ident == null)
            return new ResultInfo("450", "该员工不存在");
        offset.setIdentificationCode(ident);
        offset.setTime(new Date());
        int i = offsetService.addOffset(offset);
        if (i != 0)
            return new ResultInfo("200", "添加成功", ident);
        return new ResultInfo("500", "添加失败");
    }

    //删除报销信息
    @RequestMapping("/delOneById")
    @ResponseBody
    public String delOneById(String offsetId){
        int i = offsetService.delOneById(offsetId);
        if (i != 0)
            return "200";
        return "500";
    }
}
